//
// Copyright (C) 2015 Twitter, Inc. and other contributors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//         http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//

import Foundation
import UIKit

extension UIColor {
    class func furniOrangeColor() -> UIColor {
        return UIColor(red: 1, green: 127/255, blue: 0, alpha: 1.0)
    }

    class func furniBeigeColor() -> UIColor {
        return UIColor(red: 248/255, green: 248/255, blue: 246/255, alpha: 1.0)
    }

    class func furniBrownColor() -> UIColor {
        return UIColor(red: 183/255, green: 179/255, blue: 163/255, alpha: 1.0)
    }

    class func furniDarkGrayColor() -> UIColor {
        return UIColor(red: 42/255, green: 48/255, blue: 51/255, alpha: 1.0)
    }
    
    class func mainColor() -> UIColor {
        return UIColor(red: 41/255, green: 162/255, blue: 239/255, alpha: 1.0)
    }
    
    class func subMainColor() -> UIColor {
        return UIColor(red: 246/255, green: 142/255, blue: 105/255, alpha: 1.0)
    }
    
    class func navColor() -> UIColor {  // 导航栏颜色
        return UIColor(hex: 0x373737)!
    }
    
    class func viewBackColor() -> UIColor {
        return UIColor(hex: 0xeeeeee)!
    }
    
}




extension String {
    // Remove some occurrences of characters in a string.
    func stringByRemovingOccurrencesOfCharacters(_ chars: String) -> String {
        let cs = characters.filter {
            chars.characters.index(of: $0) == nil
        }
        return String(cs)
    }
    
    func isEmail() -> Bool {
        //验证方式
        let mailPattern = "^([a-z0-9_\\.-]+)@([\\da-z\\.-]+)\\.([a-z\\.]{2,6})$"
        let matcher:RegexHelper
        
        do {
            try matcher = RegexHelper(mailPattern)
            
            if matcher.match(self) {
                return true
            } else {
                return false
            }
            
        } catch _ {
            return false
        }
    }
    
    struct RegexHelper {
        let regex: NSRegularExpression
        
        init(_ pattern: String) throws {
            try regex = NSRegularExpression(pattern: pattern,
                                            options: .caseInsensitive)
        }
        
        func match(_ input: String) -> Bool {
            let matches = regex.matches(in: input,
                                                options: [],
                                                range: NSMakeRange(0, input.characters.count))
            return matches.count > 0
        }
    }
    
}

extension UIButton {
    // Decorate a Furni button.
    func decorateForFurni() {
        self.layer.masksToBounds = false
        self.layer.cornerRadius = 6
    }
}

extension UIView {
    // Draw a border at the top of a view.
    func drawTopBorderWithColor(_ color: UIColor, height: CGFloat) {
        let topBorder = CALayer()
        topBorder.backgroundColor = color.cgColor
        topBorder.frame = CGRect(x: 0, y: 0, width: self.bounds.width, height: height)
        self.layer.addSublayer(topBorder)
    }
}

extension UIStoryboard {
    static var mainStoryboard: UIStoryboard {
        return UIStoryboard(name: "Main", bundle: nil)
    }
}

extension UIImage {
    static func favoriteImageForFavoritedState(_ favorited: Bool) -> UIImage {
        let imageName: String

        if favorited {
            imageName = "Favorite-Selected"
        } else {
            imageName = "Favorite"
        }

        return UIImage(named: imageName)!
    }
}
